package jjj.jjjj;

import demo.controller.UserController;
import demo.dao.UserMapper;
import jdbc.DBUtil;
import jdbc.InputUtil;
import jdbc.UserInfo;
import sun.swing.SwingUtilities2;

import java.util.Scanner;

public class jj1 {
    private UserMapper userMapper=new UserMapper();
    public void start(){
        System.out.println("欢迎使用系统，请输入编号：");
        System.out.println("1.注册用户");
        System.out.println("2.登录系统");
        System.out.println("3.修改密码");
        Scanner scanner=new Scanner(System.in);
        String num=scanner.nextLine();
        switch (num){
            case "1":
                regUser();
                break;
            case "2":
                loginUser();
                break;
            case "3":
                changePWD();
                break;
            default:
                System.out.println("输入编号不正确！");
        }

    }
    public void regUser(){
        UserInfo userInfo= InputUtil.getInputObject(new UserInfo(),"注册功能","注册");
        UserMapper userMapper=new UserMapper();
        if (userMapper.findByUsername(userInfo.getUsername())!=null){
            System.out.println("用户名已存在");
        }else{
            userMapper.insert(userInfo);
            System.out.println("注册成功");
        }

    }
    public void loginUser(){

        System.out.println("请输入用户名：");
        Scanner scan=new Scanner(System.in);
        String username=scan.nextLine();
        System.out.println("请输入密码：");
        String password=scan.nextLine();
        UserInfo userInfo=userMapper.findByUsername(username);
        if (userInfo==null||!userInfo.getPassword().equals(password)){
            System.out.println("用户名或密码不正确");
        }else{
            System.out.println("登录成功");
        }


    }
    public void changePWD(){

        System.out.println("请输入用户名：");
        Scanner scan=new Scanner(System.in);
        String username=scan.nextLine();
        System.out.println("请输入旧密码：");
        String oldPassword=scan.nextLine();
        System.out.println("请输入新密码：");
        String newPassword=scan.nextLine();
        UserInfo userInfo=userMapper.findByUsername(username);
        if (userInfo==null||!userInfo.getPassword().equals(oldPassword)){
            System.out.println("用户名或密码不正确");
        }else{
            userInfo.setPassword(newPassword);
            userMapper.update(userInfo);
            System.out.println("修改密码成功");
        }
        }


    public static void main(String[] args) {
        UserController userController=new UserController();
        userController.start();
    }
}
